package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.UacGroupIpUserMapper;
import com.csun.cmny.provider.model.domain.UacGroupIpUser;
import com.csun.cmny.provider.model.vo.ipauth.IpUserVo;
import com.csun.cmny.provider.service.UacGroupIpUserService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2020-09-30 14:31
 * @Description:
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class UacGroupIpUserServiceImpl extends BaseService<UacGroupIpUser> implements UacGroupIpUserService {

    @Resource
    private UacGroupIpUserMapper uacGroupIpUserMapper;

    /**
     * select by group id
     *
     * @param groupId the group id
     *
     * @return the list
     */
    @Override
    public List<IpUserVo> selectByGroupId(Long groupId) {

        return uacGroupIpUserMapper.selectByGroupId(groupId);
    }

    /**
     * set ip auth
     *
     * @param ipUserVoList the ip user vo list
     * @param loginAuthDto the login auth dto
     *
     * @return the int.
     */
    @Override
    public Integer setIpAuth(List<IpUserVo> ipUserVoList, LoginAuthDto loginAuthDto) {

        // 根据groupId删除
        Integer result = uacGroupIpUserMapper.deleteByGroupId(loginAuthDto.getGroupId());

        List<UacGroupIpUser> uacGroupIpUserList = Lists.newArrayList();
        for (IpUserVo v : ipUserVoList) {
            UacGroupIpUser uacGroupIpUser = new UacGroupIpUser();
            uacGroupIpUser.setUpdateInfo(loginAuthDto);
            uacGroupIpUser.setGroupId(loginAuthDto.getGroupId());
            uacGroupIpUser.setUserId(v.getUserId());
            uacGroupIpUser.setNumber(v.getNumber());
            uacGroupIpUser.setRemark(v.getRemark());

            uacGroupIpUserList.add(uacGroupIpUser);
        }

        return batchSave(uacGroupIpUserList);
    }

    /**
     * delete by group id
     *
     * @param groupId the group id
     *
     * @return the int.
     */
    @Override
    public Integer deleteByGroupId(Long groupId) {

        return uacGroupIpUserMapper.deleteByGroupId(groupId);
    }

    /**
     * save uac group ip addr
     *
     * @param uacGroupIpUser the uac group ip user
     * @param loginAuthDto   the login auth dto
     * @return the int.
     */
    @Override
    public Integer save(UacGroupIpUser uacGroupIpUser, LoginAuthDto loginAuthDto) {

        uacGroupIpUser.setUpdateInfo(loginAuthDto);
        uacGroupIpUser.setGroupId(loginAuthDto.getGroupId());

        if (uacGroupIpUser.isNew()) {

            return uacGroupIpUserMapper.insertSelective(uacGroupIpUser);
        }

        return uacGroupIpUserMapper.updateByPrimaryKeySelective(uacGroupIpUser);
    }
}
